package com.yhy.tradesystem.buysellsignal;

import com.yhy.tradesystem.TradeDataPak;
import com.yhy.tradesystem.TradeDataPak.StockData;
import com.yhy.tradesystem.util.Log;

/**
 * 一个简单的快线上穿慢线买入，快线下穿慢线卖出的技术指标
 * */
public class SimpleCrossBuySellSignal extends BaseBuySellSignalFilter {
	public final static String PARAMETER_FAST_LINE = "快线";
	public final static String PARAMETER_SLOW_LINE = "慢线";
	
	private String fast_line = null;
	private String slow_line = null;
	
	private float last_fast = -1f;
	private float last_slow = -1f;
	
	private boolean firstValue = true;
	
	final String TAG="SimpleCrossBuySellSignal";
	
	
	@Override
	public void setStringParameters(String name, String value) {
		if(PARAMETER_FAST_LINE.equals(name)){
			fast_line = value;
		}
		
		if(PARAMETER_SLOW_LINE.equals(name)){
			slow_line = value;
		}
	}
	
	@Override
	public void inputData(TradeDataPak data) {
		super.inputData(data);
		
		outputData();
	}

	@Override
	public void resetData() {
		super.resetData();
		firstValue = false;
	}

	@Override
	public TradeDataPak outputData() {
		float fast = pak.pickValueFromTradeDataPak(fast_line);
		float slow = pak.pickValueFromTradeDataPak(slow_line);
		
		if(firstValue){
			last_fast = fast;
			last_slow = slow;
			firstValue = false;
		}else{
			TradeDataPak.BuySellSignal signal = null;
			
			if(last_fast < last_slow && fast > slow){
				//上穿
				signal = new TradeDataPak.BuySellSignal(pak.stockData.stockName, 1, TradeDataPak.BuySellSignal.BuySellType.BuySellOpen,pak.stockData.close);
				Log.d(TAG, pak.stockData.date+"买入"+signal.stockName+" "+String.format("%s %f %s %f", fast_line,fast,slow_line,slow));
			}
			
			if(last_fast > last_slow && fast < slow){
				//下穿
				signal = new TradeDataPak.BuySellSignal(pak.stockData.stockName, 1, TradeDataPak.BuySellSignal.BuySellType.BuySellClose,pak.stockData.close);
				Log.d(TAG, pak.stockData.date+"卖出"+signal.stockName+" "+String.format("%s %f %s %f", fast_line,fast,slow_line,slow));
			}
			
			pak.signal = signal;
			
			last_fast = fast;
			last_slow = slow;
		}
		return super.outputData();
	}

	@Override
	public String getName() {
		return fast_line+" "+slow_line+"穿越买卖策略";
	}

}
